<?php

namespace App\Http\Controllers;

use App\Exceptions\BaseException;
use App\Http\Requests\UserRequest;
use App\Services\User;
use Illuminate\Http\Request;


class UserController extends Controller
{

    protected $userService;

    public function __construct()
    {
        parent::__construct();
        $this->userService = new User();
    }

    /**
     * user list
     *
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        try{
            $limit = intval($request->get('count',12));
            $filter = $request->get('filter');
            $users = $this->userService->getList($limit, $filter['email']);
            return $this->success('',$users);
        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }

    /**
     * user show
     *
     * @param $id
     * @return array
     */
    public function show($id)
    {
        try{
            $id = intval($id);
            $user = $this->userService->show($id);
            return $this->success('', $user);
        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }

    /**
     * user create
     *
     * @param UserRequest $request
     * @return array
     */
    public function store(UserRequest $request)
    {
        try{
            $name = trim($request->get('name'));
            $email = trim($request->get('email'));
            $type = intval($request->get('type'));
            $password = trim($request->get('password'));
            $this->userService->create(compact('name', 'email', 'password', 'type'));
            return $this->success('新增用户成功!');

        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }

    /**
     * user update
     *
     * @param $id
     * @param UserRequest $request
     * @return array
     */
    public function update($id,UserRequest $request)
    {
        try{
            $id = intval($id);
            $name = trim($request->get('name'));
            $email = trim($request->get('email'));
            $type = intval($request->get('type'));
            $password = trim($request->get('password'));
            $this->userService->update($id,compact('name', 'email', 'password', 'type'));
            return $this->success('用户已更新!');
        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }

    /**
     * 获取库存管理员列表
     *
     * @return array
     */
    public function getCanteenManagers()
    {
        try{
           $managers =  $this->userService->getCanteenManagers();
           return $this->success('', $managers);
        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }
}
